//
//  AddNameAndCardTableViewController.m
//  EasyToTesco2.0
//
//  Created by knew on 16/11/10.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "AddNameAndCardTableViewController.h"
#import "LogInPassWordFooterView.h"
#import "BindingBankCardTableViewCell.h"

@interface AddNameAndCardTableViewController ()<didClickBindingBankCardButtonDelegate,LogInPassWordFooterDelegate>

@property (nonatomic,strong)NSArray *PlaceholderArray;

@property (nonatomic,strong)UITextField *textField;

@property (nonatomic,strong)NSMutableArray *textFieldArray;

@end

@implementation AddNameAndCardTableViewController

- (NSMutableArray *)textFieldArray {
    if (!_textFieldArray) {
        _textFieldArray = [NSMutableArray array];
    }
    return _textFieldArray;
}

- (NSArray *)PlaceholderArray {
    if (!_PlaceholderArray) {
        _PlaceholderArray = @[
                              @{
                                  @"label":@"真实姓名",
                                  @"place":@"请输入您的真实姓名"
                                  },
                              @{
                                  @"label":@"身份证",
                                  @"place":@"请输入您的身份证号"
                                  }
                              ];
        
    }
    return _PlaceholderArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"绑定个人信息";
    
    self.tableView.backgroundColor = BACKGROUND_COLOR;
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, -25, 0, 0)];
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BindingBankCardTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (!cell) {
        cell = [[BindingBankCardTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.leftLabel.text = [self.PlaceholderArray[indexPath.row] objectForKey:@"label"];
    cell.rightTextField.placeholder = [self.PlaceholderArray[indexPath.row] objectForKey:@"place"];;
    
    if (indexPath.row == 1) {
        cell.rightTextField.keyboardType = UIKeyboardTypeNumberPad;
    }
    cell.rightTextField.tag = indexPath.row;
    
    [self.textFieldArray addObject:cell.rightTextField];
    
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return HEIGHT/5;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    LogInPassWordFooterView *footer = [[LogInPassWordFooterView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/5)];
    footer.delegate = self;
    return footer;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self clearResignFirstResponder];
}

- (void)clearResignFirstResponder {
    for (UITextField *textField in self.textFieldArray) {
        [textField resignFirstResponder];
    }
}

- (void)LogInPassWordFooterViewButton:(UIButton *)button {
    [self clearResignFirstResponder];
    
    UITextField *bankNumber = self.textFieldArray.firstObject;
    
    UITextField *phoneNumber = self.textFieldArray[1];
    
    if ((bankNumber.text.length == 0) || (phoneNumber.text.length == 0)) {
        
        MBProgressHUD *hud = [MBProgressHUD showMessage:@"请确保信息输入无误" toView:self.view dimBackground:NO AndCompleteBlock:nil];
        [self.view addSubview:hud];
        return;
    }
    
    NSDictionary *params = @{
                             @"username":USERNAME,
                             @"xingming":bankNumber.text,
                             @"shenfen":phoneNumber.text
                             };
    
    NSLog(@"%@",params);
                
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/User/addUserInfo" completeBlock:^(id object) {
        
        [JudgeBackDataCodeMessage JudgeBackDataCodeMessage:@"绑定个人信息成功" andObjece:object andAddHUDView:self.view andPopViewController:^(id object) {
            [self.navigationController popViewControllerAnimated:YES];
        }];
        
    }];
}

@end
